package com.qiaofang.controller;

import com.alibaba.excel.EasyExcel;
import com.qiaofang.common.response.CommonResponse;
import com.qiaofang.common.response.SuccessfulResponseFactory;
import com.qiaofang.service.BatchService;
import com.qiaofang.util.BatchReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@RestController
@Api(tags = {"报备批量导入"})
@RequestMapping(value = "/batch", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public class BatchController {
    private static Logger logger= LoggerFactory.getLogger(BatchController.class);

    @Autowired
    private BatchService batchService;

    @PostMapping(value = "batchReport")
    @ApiOperation(value = "报备列表")
    public CommonResponse batchReport(@RequestParam(value = "file", required = false) MultipartFile file, Long companyId, String reportDate) {
        batchService.batchReport(file, companyId, reportDate);
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @GetMapping("downloadFailExcel")
    public void downloadFailExcel(HttpServletResponse response) throws IOException {
        List<BatchReportVO> vos = batchService.downloadFailExcel();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=导入失败数据.xlsx");
        EasyExcel.write(response.getOutputStream(), BatchReportVO.class).sheet("模板").doWrite(vos);
    }

    @GetMapping("downloadModelExcel")
    public void downloadModelExcel(HttpServletResponse response) throws IOException {
        try {
            Resource resource = new ClassPathResource("poi/batch-model.xlsx");
            InputStream inputStream = resource.getInputStream();
            //强制下载不打开
            response.setContentType("application/force-download");
            OutputStream out = response.getOutputStream();
            //使用URLEncoder来防止文件名乱码或者读取错误
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=批量导入模板.xlsx");
            int b = 0;
            byte[] buffer = new byte[1000000];
            while (b != -1) {
                b = inputStream.read(buffer);
                if (b != -1) {
                    out.write(buffer, 0, b);
                }
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            logger.error("downloadModelExcel error: {}", e);
        }
    }

}
